package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import java.math.BigDecimal;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllFoodbatchs", query = "select myFoodbatch from Foodbatch myFoodbatch"),
		@NamedQuery(name = "findFoodbatchByName", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.name = ?1"),
		@NamedQuery(name = "findFoodbatchByNameContaining", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findFoodbatchByBatch", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.batch = ?1"),
		@NamedQuery(name = "findFoodbatchByBatchContaining", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.batch like concat('%',?1,'%')"),
		@NamedQuery(name = "findFoodbatchByDate", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.date = ?1"),
		@NamedQuery(name = "findFoodbatchByPrice", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.price = ?1"),
		@NamedQuery(name = "findFoodbatchByDescription", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.description = ?1"),
		@NamedQuery(name = "findFoodbatchByDescriptionContaining", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllFoodbatchsByDetectmethod", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.detectmethod.id = ?1"),
		@NamedQuery(name = "findAllFoodbatchsByFood", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.food.id = ?1"),
		@NamedQuery(name = "findAllFoodbatchsByUnit", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.unit.id = ?1"),
		@NamedQuery(name = "findFoodbatchById", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.id = ?1"),
		@NamedQuery(name = "findFoodbatchByPrimaryKey", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.id = ?1 "),
		@NamedQuery(name = "deleteFoodbatchsByIds", query = "delete Foodbatch where id in (?1)"),
		@NamedQuery(name = "findFoodbatchsByIds", query = "select myFoodbatch from Foodbatch myFoodbatch where myFoodbatch.id in (?1)"),
		@NamedQuery(name = "findFoodbatchByPhyscialPrimaryKey", query = "select myFoodbatch from Foodbatch myFoodbatch where  myFoodbatch.food.id = ?1 and myFoodbatch.date = ?2"),
		@NamedQuery(name = "countFoodbatchsByName", query = "select count(myFoodbatch)  from Foodbatch myFoodbatch where myFoodbatch.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "foodbatch")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Foodbatch")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Foodbatch implements Serializable, Comparable<Foodbatch> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "batch")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String batch;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar date;

	@Column(name = "price", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal price;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "detectmethod", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Detectmethod detectmethod;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "food", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Food food;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "unit", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Unit unit;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "foodbatch", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detect> detectsForFoodbatch;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "foodbatch", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Event> eventsForFoodbatch;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "foodbatch", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodbatchadditive> foodbatchadditivesForFoodbatch;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "foodbatch", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodbatchfiller> foodbatchfillersForFoodbatch;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "foodbatch", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodbatchmaterial> foodbatchmaterialsForFoodbatch;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setBatch(String batch) {
		this.batch = batch;
	}

	public String getBatch() {
		return this.batch;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	public Calendar getDate() {
		return this.date;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public BigDecimal getPrice() {
		return this.price;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDetectmethod(Detectmethod detectmethod) {
		this.detectmethod = detectmethod;
	}

	public Detectmethod getDetectmethod() {
		return detectmethod;
	}

	public void setFood(Food food) {
		this.food = food;
	}

	public Food getFood() {
		return food;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public Unit getUnit() {
		return unit;
	}

	public void setDetectsForFoodbatch(Set<Detect> detectsForFoodbatch) {
		this.detectsForFoodbatch = detectsForFoodbatch;
	}

	public Set<Detect> getDetectsForFoodbatch() {
		if (detectsForFoodbatch == null) {
			detectsForFoodbatch = new java.util.LinkedHashSet<Detect>();
		}
		return detectsForFoodbatch;
	}

	public void setEventsForFoodbatch(Set<Event> eventsForFoodbatch) {
		this.eventsForFoodbatch = eventsForFoodbatch;
	}

	public Set<Event> getEventsForFoodbatch() {
		if (eventsForFoodbatch == null) {
			eventsForFoodbatch = new java.util.LinkedHashSet<Event>();
		}
		return eventsForFoodbatch;
	}

	public void setFoodbatchadditivesForFoodbatch(Set<Foodbatchadditive> foodbatchadditivesForFoodbatch) {
		this.foodbatchadditivesForFoodbatch = foodbatchadditivesForFoodbatch;
	}

	public Set<Foodbatchadditive> getFoodbatchadditivesForFoodbatch() {
		if (foodbatchadditivesForFoodbatch == null) {
			foodbatchadditivesForFoodbatch = new java.util.LinkedHashSet<Foodbatchadditive>();
		}
		return foodbatchadditivesForFoodbatch;
	}

	public void setFoodbatchfillersForFoodbatch(Set<Foodbatchfiller> foodbatchfillersForFoodbatch) {
		this.foodbatchfillersForFoodbatch = foodbatchfillersForFoodbatch;
	}

	public Set<Foodbatchfiller> getFoodbatchfillersForFoodbatch() {
		if (foodbatchfillersForFoodbatch == null) {
			foodbatchfillersForFoodbatch = new java.util.LinkedHashSet<Foodbatchfiller>();
		}
		return foodbatchfillersForFoodbatch;
	}

	public void setFoodbatchmaterialsForFoodbatch(Set<Foodbatchmaterial> foodbatchmaterialsForFoodbatch) {
		this.foodbatchmaterialsForFoodbatch = foodbatchmaterialsForFoodbatch;
	}

	public Set<Foodbatchmaterial> getFoodbatchmaterialsForFoodbatch() {
		if (foodbatchmaterialsForFoodbatch == null) {
			foodbatchmaterialsForFoodbatch = new java.util.LinkedHashSet<Foodbatchmaterial>();
		}
		return foodbatchmaterialsForFoodbatch;
	}

	public void copy(Foodbatch that) {

		setId(that.getId());

		setName(that.getName());
		setBatch(that.getBatch());
		setDate(that.getDate());
		setPrice(that.getPrice());
		setDescription(that.getDescription());

		setDetectmethod(that.getDetectmethod());

		setFood(that.getFood());

		setUnit(that.getUnit());

		setDetectsForFoodbatch(
				new java.util.LinkedHashSet<Detect>(that.getDetectsForFoodbatch()));

		setEventsForFoodbatch(
				new java.util.LinkedHashSet<Event>(that.getEventsForFoodbatch()));

		setFoodbatchadditivesForFoodbatch(new java.util.LinkedHashSet<Foodbatchadditive>(
				that.getFoodbatchadditivesForFoodbatch()));

		setFoodbatchfillersForFoodbatch(new java.util.LinkedHashSet<Foodbatchfiller>(
				that.getFoodbatchfillersForFoodbatch()));

		setFoodbatchmaterialsForFoodbatch(new java.util.LinkedHashSet<Foodbatchmaterial>(
				that.getFoodbatchmaterialsForFoodbatch()));

	}

	public Foodbatch() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("batch=[").append(batch).append("] ");
		buffer.append("date=[").append(date).append("] ");
		buffer.append("price=[").append(price).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Foodbatch))
			return false;
		Foodbatch comparedFoodbatch = (Foodbatch) obj;
		if (!CheckPropertyEqual(this.food.id, comparedFoodbatch.food.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.date, comparedFoodbatch.date)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Foodbatch o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
